import java.io.File;
import java.io.IOException;
import java.util.List;
import java.util.Map;
import java.util.Scanner;
import java.util.concurrent.ConcurrentHashMap;

import org.apache.commons.io.FileUtils;
import org.apache.http.impl.client.IdleConnectionEvictor;

public class Main2 {
	private static final IdleConnectionEvictor EVICTOR = new IdleConnectionEvictor(HttpClientUtil.getSinglePoolingHttpClientConnectionManager(), 3,
			null);
	private static final String TOKEN = "optionOn";

	@SuppressWarnings("unchecked")
	public static void main(String[] args) {
		String URL = "http://www.baidu.com";
		//

		System.out.println("开始读取文件");
		String currentPath = Main2.class.getResource("/").getPath();
		if (currentPath.contains(":")) {
			currentPath = currentPath.substring(1);
		}
		// String name = currentPath + "Proxy.txt";
		String name = "d:/Proxy.txt";
		List<String> proxies = null;
		try {
			System.out.println("读取文件中...");
			proxies = FileUtils.readLines(new File(name));
			System.out.println("读取完毕！");
		} catch (IOException e1) {
			System.out.println("读取失败！请检查文件是否存在！[" + name + "]");
			return;
		}

		///
		System.out.println("开始验证...");
		Map<String, String> map = new ConcurrentHashMap<>();
		// EVICTOR.start();
		ThreadGroup tg = new ThreadGroup("exec");

		proxies.parallelStream().forEach((e) -> {
			// new Thread(() -> {
				HttpClientUtil.doGet2(URL, e, map);
			// }).start();
		});
		// try {
		// Thread.sleep(120 * 1000);
		// tg.interrupt();
		// } catch (InterruptedException e1) {
		// e1.printStackTrace();
		// }

		System.out.println("----------");

		// EVICTOR.shutdown();
		System.out.println("验证结束！");
		System.out.println(">>>>>>>>>>>>>>>成功次数为：" + map.size() + "<<<<<<<<<<<<<<<");

		///
		try {
			System.out.println("开始将可用代理写入原文件中...");
			FileUtils.writeLines(new File(name), map.keySet());
			System.out.println("写入成功！");
		} catch (IOException e2) {
			System.out.println("写入失败！请检查是否有权限写入该文件。");
		}
		System.out.println("执行完毕！");
		System.exit(0);
	}

	public static String needUrl() {
		System.out.println("请输入要访问的网址：");
		@SuppressWarnings("resource")
		Scanner sc = new Scanner(System.in);
		String url = sc.nextLine();
		url = url.trim();
		if (url.isEmpty()) {
			needUrl();
		}
		return url;
	}
}
